package com.sls.nzdapplication.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.style.FontStyle;
import com.bin.david.form.data.table.TableData;
import com.sls.nzdapplication.R;
import com.sls.nzdapplication.db.utils.DyEquipmentUtil;
import com.sls.nzdapplication.model.DyEquipment;
import com.sls.nzdapplication.model.DyManagementBoxData;
import com.sls.nzdapplication.model.DyManagementEquipment;
import com.sls.nzdapplication.model.Equipment;
import com.sls.nzdapplication.ui.activity.DyEquipmentActivity;
import com.sls.nzdapplication.ui.activity.PdxEquipmentActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 它是一个名为DyManagementEquipmentFragment的片段。
 * 它包含一个SmartTable，用于显示DyEquipment的列表。
 * 在onViewCreated方法中，它设置了SmartTable的一些属性，如不显示X和Y序列，不显示表格标题等。
 * 在initData方法中，它使用DyEquipmentUtil查询DyEquipment列表，并将其传递给showData方法。
 * showData方法将DyEquipment列表转换为DyManagementEquipment列表，并将其传递给SmartTable以显示。
 * 如果DyEquipment列表为空，则SmartTable将不可见。在SmartTable中，如果用户单击行，则会启动一个新的Activity，该Activity将显示所选DyEquipment的详细信息。
 * */
public class DyManagementEquipmentFragment extends Fragment {
    private DyEquipmentUtil equipmentUtil;

    private LinearLayout dataView;

    private List<DyEquipment> equipmentList;

    private String lineId;
    private SmartTable<DyManagementEquipment> table;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dy_equipment_manage_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lineId = getArguments().getString("areaId");
        table = view.findViewById(R.id.table);
        table.getConfig().setShowXSequence(false);
        table.getConfig().setShowYSequence(false);
        table.getConfig().setShowTableTitle(false);
        table.getConfig().setColumnTitleStyle (new FontStyle(30, getResources().getColor(R.color.colorPrimaryDark)));
        table.getConfig().setMinTableWidth(getActivity().getWindowManager().getDefaultDisplay().getWidth());
        initData();

    }

    private void initData(){
        equipmentUtil = new DyEquipmentUtil(getContext());
        equipmentList = equipmentUtil.queryDyEquipmentByAreaId(lineId);
        showData(equipmentList);
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    /**
     * 数据
     */
    private void showData(List<DyEquipment> titleData) {
        table.setTableData(null);
        List<DyManagementEquipment> dyManagementEquipmentList = new ArrayList<>();
        for (int i = 0; i < titleData.size(); i++) {
            final DyEquipment equipment = titleData.get(i);
            DyManagementEquipment dyManagementEquipment = new DyManagementEquipment();
            dyManagementEquipment.setXh(String.valueOf(i+1));
            dyManagementEquipment.setSbmc(equipment.getName());
            DyEquipment equipment1 = equipmentUtil.listOneDyEquipment(equipment.getPreviousEquipmentId());
            if(null!= equipment1){
                dyManagementEquipment.setSjmc(equipment1.getName());
            }else{
                dyManagementEquipment.setSjmc("");
            }
            dyManagementEquipment.setZt("正常");
            dyManagementEquipmentList.add(dyManagementEquipment);

        }

        if(dyManagementEquipmentList.size()>0){
            table.setVisibility(View.VISIBLE);
            table.setData(dyManagementEquipmentList);
            table.getTableData().setOnRowClickListener(new TableData.OnRowClickListener<DyManagementEquipment>() {
                @Override
                public void onClick(Column column, DyManagementEquipment dyManagementEquipment, int col, int row) {
                    DyEquipment equipment = titleData.get(row);
                    if(equipment.getType().equals("配电箱")){
                        Intent intent = new Intent(getActivity(), PdxEquipmentActivity.class);
                        intent.putExtra("equipmentId",equipment.getId());
                        startActivity(intent);
                    }else{
                        Intent intent = new Intent(getActivity(), DyEquipmentActivity.class);
                        intent.putExtra("equipmentId",equipment.getId());
                        startActivity(intent);
                    }
                }
            });
        }else{
            table.setVisibility(View.GONE);
        }

    }
}
